package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.CartService;
import cn.bytecollege.common.constant.Resource;
import cn.bytecollege.common.entity.Cart;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "CartDeleteController", value = "/admin/cartDelete")
public class CartDeleteController extends HttpServlet {
    private CartService cartService = null;

    public CartDeleteController() {
        cartService = new CartService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("cartId");
        Integer cartId = Integer.valueOf(id);
        Integer i = cartService.findByCartId(cartId);
        Cart cart = new Cart();
//判断数量是否大于一
        if (i > 1) {
            cart.setNumber(i - 1);
            cart.setCartId(cartId);
            cartService.updateByCartId(cart);
            response.sendRedirect(request.getContextPath() + "/admin/cartList");
            return;
        }
        cartService.deleteById(cartId);
        response.sendRedirect(request.getContextPath() + "/admin/cartList");

    }
}
